package org.laobai.permission.struct;

import java.io.Serializable;
import java.util.Objects;

// 结果集
public class Result<T> implements Serializable {
    /**
    * 业务状态码
    */
    private Integer code;

    /**
    * 当前业务状态码的解释信息
    */
    private String msg;


    /**
    * 本次请求是否成功
    */
    private Boolean flag;


    /**
    * 响应体
    */
    private T data;


    public Result() {
    }

    public Result(Integer code, String msg, Boolean flag) {
        this.code = code;
        this.msg = msg;
        this.flag = flag;
    }

    public Result(Integer code, String msg, Boolean flag, T data) {
        this.code = code;
        this.msg = msg;
        this.flag = flag;
        this.data = data;
    }

    public static <T> Result<T> of(Integer code, String msg, Boolean flag) {
        return new Result<>(code, msg, flag);

    }

    public static <T> Result<T> of(Integer code, String msg, Boolean flag, T data) {
        return new Result<>(code, msg, flag, data);

    }

    public static <T> Result<T> ok(T t) {
        Result<T> result = new Result<>(200, "ok", true);
        if (Objects.nonNull(t)) {
            result.setData(t);
        }
        return result;
    }

    public static <T> Result<T> ok() {
        return Result.ok(null);
    }


    public static <T> Result<T> error(T t) {
        Result<T> result = new Result<>(500, "error", false);
        if (Objects.nonNull(t)) {
            result.setData(t);
        }
        return result;
    }

    public static <T> Result<T> error() {
        return Result.error(null);
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Boolean getFlag() {
        return flag;
    }

    public void setFlag(Boolean flag) {
        this.flag = flag;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
